﻿using System;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyTuyenSinh.BussinessLayer;
using QuanLyTuyenSinh.Properties;

namespace QuanLyTuyenSinh.PresentationLayer
{
    public partial class frmThiSinh : Office2007Form
    {
        public int load = 0;
        public int loadStydy = 0;
        public int loadStydyMatinh = 0;
        public int loadStydyMatinh10 = 0;
        public int loadStydyMatinh11 = 0;
        public int loadStydyMatinh12 = 0;
        public int loadStydyMatruong10 = 0;
        public int loadStydyMatruong11 = 0;
        public int loadStydyMatruong12 = 0;
        public int loadStydyNV = 0;
        private int ma;

        public frmThiSinh()
        {
            InitializeComponent();
        }

        private void HideControl(bool hien)
        {
            cmdThem.Visible = hien;
            cmdLuu.Visible = hien;
            cmdXoa.Visible = hien;
            cmdThem.Enabled = true;
            cmdXoa.Enabled = true;
        }

        private void frmThiSinh_Load(object sender, EventArgs e)
        {
            try
            {
                #region Load Trường Đại học, cao đẳng

                DataTable listUniversity = Operator.Operator.GetListUniversity();
                DataTable listCD = Operator.Operator.GetListUniversity();
                if (listUniversity == null) return;
                if (listUniversity.Rows.Count > 0)
                {
                    load = 0;
                    cbUniversityDisplayNV.DataSource = listUniversity;
                    cbUniversityDisplayNV.ValueMember = "Id";
                    cbUniversityDisplayNV.DisplayMember = "UniversityDisplay";
                }
                load = 1;
                if (listUniversity.Rows.Count > 1)
                {
                    cbUniversityDisplayNV.SelectedIndex = 1;
                    cbUniversityDisplayNV.SelectedIndex = 0;
                }

                if (listCD == null) return;
                if (listCD.Rows.Count > 0)
                {
                    cbTruongCD.DataSource = listCD;
                    cbTruongCD.ValueMember = "Id";
                    cbTruongCD.DisplayMember = "UniversityDisplay";
                }
                if (listCD.Rows.Count > 1)
                {
                    cbTruongCD.SelectedIndex = 1;
                    cbTruongCD.SelectedIndex = 0;
                }

                #endregion

                cbGioiTinh.SelectedIndex = 1;

                #region Load dân tộc

                DataTable listEthnic = Operator.Operator.GetListEthnic();
                if (listEthnic == null) return;
                if (listEthnic.Rows.Count > 0)
                {
                    cbDanToc.DataSource = listEthnic;
                    cbDanToc.ValueMember = "EthnicId";
                    cbDanToc.DisplayMember = "EthnicName";
                }

                #endregion

                #region Load ưu tiên

                DataTable listPriority = Operator.Operator.GetListPriority();
                if (listPriority == null) return;
                if (listPriority.Rows.Count > 0)
                {
                    cbDoiTuong.DataSource = listPriority;
                    cbDoiTuong.ValueMember = "Id";
                    cbDoiTuong.DisplayMember = "PriorityDisplay";
                }

                #endregion

                #region Load mã tỉnh, mã tỉnh 10, 11,12

                DataTable listProvince = Operator.Operator.GetListProvince();
                DataTable list1 = Operator.Operator.GetListProvince();
                DataTable list2 = Operator.Operator.GetListProvince();
                DataTable list3 = Operator.Operator.GetListProvince();

                if (listProvince.Rows.Count > 0)
                {
                    loadStydyMatinh = 0;
                    cbMaTinh.DataSource = listProvince;
                    cbMaTinh.ValueMember = "Id";
                    cbMaTinh.DisplayMember = "ProvinceDisplay";
                    loadStydyMatinh = 1;
                    if (cbMaTinh.Items.Count > 1)
                    {
                        cbMaTinh.SelectedIndex = 1;
                        cbMaTinh.SelectedIndex = 0;
                    }
                }


                if (list1.Rows.Count > 0)
                {
                    loadStydyMatinh10 = 0;
                    cbMaTinh10.DataSource = list1;
                    cbMaTinh10.ValueMember = "Id";
                    cbMaTinh10.DisplayMember = "ProvinceDisplay";
                    loadStydyMatinh10 = 1;
                    if (cbMaTinh10.Items.Count > 1)
                    {
                        cbMaTinh10.SelectedIndex = 1;
                        cbMaTinh10.SelectedIndex = 0;
                    }
                }

                if (list2.Rows.Count > 0)
                {
                    loadStydyMatinh11 = 0;
                    cbMaTinh11.DataSource = list2;
                    cbMaTinh11.ValueMember = "Id";
                    cbMaTinh11.DisplayMember = "ProvinceDisplay";
                    loadStydyMatinh11 = 1;
                    if (cbMaTinh11.Items.Count > 1)
                    {
                        cbMaTinh11.SelectedIndex = 1;
                        cbMaTinh11.SelectedIndex = 0;
                    }
                }


                if (list3.Rows.Count > 0)
                {
                    loadStydyMatinh12 = 0;
                    cbMaTinh12.DataSource = list3;
                    cbMaTinh12.ValueMember = "Id";
                    cbMaTinh12.DisplayMember = "ProvinceDisplay";
                    loadStydyMatinh12 = 1;
                    if (cbMaTinh12.Items.Count > 1)
                    {
                        cbMaTinh12.SelectedIndex = 1;
                        cbMaTinh12.SelectedIndex = 0;
                    }
                }

                #endregion

                cbNamTN.SelectedIndex = 1;
                CbNamTNCD.SelectedIndex = 1;

                #region Load khu vực

                DataTable listZone = Operator.Operator.GetListZone();
                if (listZone == null) return;
                if (listZone.Rows.Count > 0)
                {
                    cbKhuVuc.DataSource = listZone;
                    cbKhuVuc.ValueMember = "Id";
                    cbKhuVuc.DisplayMember = "ZoneDisplay";
                }

                #endregion

                #region Load nơi nộp hồ sơ

                DataTable listHighSchool = Operator.Operator.GetListHighSchool();
                if (listHighSchool == null) return;
                if (listHighSchool.Rows.Count > 0)
                {
                    CbPlaceOfHS.DataSource = listHighSchool;
                    CbPlaceOfHS.ValueMember = "Id";
                    CbPlaceOfHS.DisplayMember = "HighSchoolName";
                }

                #endregion

                #region Load mã ngành

                DataTable listUn = Operator.Operator.GetListUniversityByDisplay("SP2");
                if (listUn.Rows.Count > 0)
                {
                    int id = int.Parse(listUn.Rows[0][0].ToString().Trim());
                    DataTable listStudy = Operator.Operator.GetListStudyByUniversityId(id);
                    if (listStudy.Rows.Count <= 0) return;
                    loadStydyNV = 0;
                    cbStudyDisplay.DataSource = listStudy;
                    cbStudyDisplay.ValueMember = "Id";
                    cbStudyDisplay.DisplayMember = "StudyDisplay";
                    loadStydyNV = 1;
                    if (cbStudyDisplay.Items.Count > 1)
                    {
                        cbStudyDisplay.SelectedIndex = 1;
                        cbStudyDisplay.SelectedIndex = 0;
                    }
                }

                #endregion

                HideControl(false);
                cmdThem.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void CbUniversityDisplayNvSelectedIndexChanged(object sender, EventArgs e)
        {
            if (load == 0) return;
            if (cbUniversityDisplayNV.SelectedValue != null)
            {
                cbStudyDisplayNV.Text = "";
                txtuniversityName.Text = "";
                var university = new University();
                university.Id = int.Parse(cbUniversityDisplayNV.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListUniversityById(university.Id);
                if (listUniversity.Rows.Count > 0)
                {
                    txtuniversityName.Text = listUniversity.Rows[0][2].ToString().Trim();
                }
                DataTable list = Operator.Operator.GetListStudyByUniversityId(university.Id);
                if (list.Rows.Count > 0)
                {
                    loadStydy = 0;
                    cbStudyDisplayNV.DataSource = list;
                    cbStudyDisplayNV.ValueMember = "Id";
                    cbStudyDisplayNV.DisplayMember = "StudyDisplay";
                }
                loadStydy = 1;
                if (cbStudyDisplayNV.Items.Count <= 1) return;
                cbStudyDisplayNV.SelectedIndex = 1;
                cbStudyDisplayNV.SelectedIndex = 0;
            }
        }

        private void CbStudyDisplayNvSelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydy == 0) return;
            cbBlockDisplayNV.Text = "";
            txtStudyNameNv.Text = "";
            if (cbBlockDisplayNV.Items.Count > 0) cbBlockDisplayNV.Items.Clear();
            if ((cbStudyDisplayNV.SelectedValue != null))
            {
                var study = new Study();
                study.UniversityId = int.Parse(cbUniversityDisplayNV.SelectedValue.ToString());
                study.Id = (int) cbStudyDisplayNV.SelectedValue;
                DataTable list = Operator.Operator.GetListStudyByIdUniversityId(study.UniversityId, study.Id);
                if (list.Rows.Count > 0)
                {
                    txtStudyNameNv.Text = list.Rows[0][3].ToString().Trim();
                    txtKhoiThiNV.Text = list.Rows[0][4].ToString().Trim();
                    string[] a = txtKhoiThiNV.Text.Split(',');
                    for (int i = 0; i < a.Length; i++)
                    {
                        cbBlockDisplayNV.Items.Add(a[i]);
                    }
                }
            }
        }

        private void CbMaTinhSelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatinh == 0) return;
            if (cbMaTinh.SelectedValue != null)
            {
                cbMaHuyen.Text = "";
                var university = new District();
                university.Id = int.Parse(cbMaTinh.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListDistrictByProvinceId(university.Id);
                if (listUniversity.Rows.Count <= 0) return;
                if (listUniversity.Rows.Count > 0)
                {
                    cbMaHuyen.DataSource = listUniversity;
                    cbMaHuyen.ValueMember = "Id";
                    cbMaHuyen.DisplayMember = "DistrictDisplay";
                }
                if (cbMaHuyen.Items.Count <= 1) return;
                cbMaHuyen.SelectedIndex = 1;
                cbMaHuyen.SelectedIndex = 0;
            }
        }

        private void CbMaTinh10SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatinh10 == 0) return;
            if (cbMaTinh10.SelectedValue != null)
            {
                cbMaTruong10.Text = "";
                var university = new District();
                university.Id = int.Parse(cbMaTinh10.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolByProvinceId(university.Id);
                if (listUniversity.Rows.Count <= 0) return;
                if (listUniversity.Rows.Count > 0)
                {
                    cbMaTruong10.DataSource = listUniversity;
                    cbMaTruong10.ValueMember = "Id";
                    cbMaTruong10.DisplayMember = "HighSchoolDisplay";
                    loadStydyMatruong10 = 0;
                }
                loadStydyMatruong10 = 1;
                if (cbMaTruong10.Items.Count > 1)
                {
                    cbMaTruong10.SelectedIndex = 1;
                    cbMaTruong10.SelectedIndex = 0;
                }
            }
        }

        private void CbMaTinh11SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatinh11 == 0) return;
            if (cbMaTinh11.SelectedValue != null)
            {
                cbMaTruong11.Text = "";
                var university = new District();
                university.Id = int.Parse(cbMaTinh11.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolByProvinceId(university.Id);
                if (listUniversity.Rows.Count <= 0) return;
                if (listUniversity.Rows.Count > 0)
                {
                    cbMaTruong11.DataSource = listUniversity;
                    cbMaTruong11.ValueMember = "Id";
                    cbMaTruong11.DisplayMember = "HighSchoolDisplay";
                    loadStydyMatruong11 = 0;
                }
                loadStydyMatruong11 = 1;
                if (cbMaTruong11.Items.Count <= 1) return;

                cbMaTruong11.SelectedIndex = 1;
                cbMaTruong11.SelectedIndex = 0;
            }
        }

        private void CbMaTinh12SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatinh12 == 0) return;
            if (cbMaTinh12.SelectedValue != null)
            {
                cbMaTruong12.Text = "";
                var university = new District();
                university.Id = int.Parse(cbMaTinh12.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolByProvinceId(university.Id);
                if (listUniversity.Rows.Count <= 0) return;
                if (listUniversity.Rows.Count > 0)
                {
                    cbMaTruong12.DataSource = listUniversity;
                    cbMaTruong12.ValueMember = "Id";
                    cbMaTruong12.DisplayMember = "HighSchoolDisplay";
                    loadStydyMatruong12 = 0;
                }
                loadStydyMatruong12 = 1;
                if (cbMaTruong12.Items.Count <= 1) return;

                cbMaTruong12.SelectedIndex = 1;
                cbMaTruong12.SelectedIndex = 0;
            }
        }

        private void CbMaTruong10SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatruong10 == 0) return;
            if (cbMaTruong10.SelectedValue != null)
            {
                txtLop10.Text = "";
                var university = new HighSchool();
                university.Id = int.Parse(cbMaTruong10.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolById(university.Id);
                if (listUniversity.Rows.Count > 0)
                {
                    txtLop10.Text = listUniversity.Rows[0][4].ToString().Trim();
                }
            }
        }

        private void CbMaTruong11SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatruong11 == 0) return;
            if (cbMaTruong11.SelectedValue != null)
            {
                txtLop11.Text = "";
                var university = new HighSchool();
                university.Id = int.Parse(cbMaTruong11.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolById(university.Id);
                if (listUniversity.Rows.Count > 0)
                {
                    txtLop11.Text = listUniversity.Rows[0][4].ToString().Trim();
                }
            }
        }

        private void CbMaTruong12SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatruong12 == 0) return;
            if (cbMaTruong12.SelectedValue != null)
            {
                txtLop12.Text = "";
                var university = new HighSchool();
                university.Id = int.Parse(cbMaTruong12.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolById(university.Id);
                if (listUniversity.Rows.Count > 0)
                {
                    txtLop12.Text = listUniversity.Rows[0][4].ToString().Trim();
                }
            }
        }

        private void CbStudyDisplaySelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyNV == 0 || cbStudyDisplay.SelectedItem == null) return;
            var study = new Study();
            if (cbBlockName.Items.Count > 0)
                cbBlockName.Items.Clear();
            DataTable listUn = Operator.Operator.GetListUniversityByDisplay("SP2");
            if (listUn.Rows.Count <= 0) return;
            study.UniversityId = int.Parse(listUn.Rows[0][0].ToString().Trim());
            study.Id = (int) cbStudyDisplay.SelectedValue;
            DataTable list = Operator.Operator.GetListStudyByIdUniversityId(study.UniversityId, study.Id);
            if (list.Rows.Count > 0)
            {
                txtStudyName.Text = list.Rows[0][3].ToString().Trim();
                txtKhoithi.Text = list.Rows[0][4].ToString().Trim();
                string[] a = txtKhoithi.Text.Split(',');
                for (int i = 0; i < a.Length; i++)
                {
                    cbBlockName.Items.Add(a[i]);
                }
            }
        }

        private void CmdThemClick(object sender, EventArgs e)
        {
            HideControl(true);
            ma = 0;
            cmdXoa.Visible = false;
            txtStudyOther.Clear();
            txtStudyOtherNv.Clear();
            txtHoten.Clear();
            txtNoiSinh.Clear();
            txtHoKhau.Clear();
            txtCardNo.Clear();
            txtMobile.Clear();
        }

        private void GrHoSoTsCellClick(object sender, DataGridViewCellEventArgs e)
        {
            HideControl(true);
        }

        private void frmThiSinh_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Control) && (e.KeyCode == Keys.S) && cmdLuu.Visible)
            {
                CmdLuuClick(sender, e);
            }
            else if (e.Control && e.KeyCode == Keys.N)
            {
                CmdThemClick(sender, e);
            }
            else if ((e.KeyCode == Keys.Delete) && (cmdXoa.Visible))
            {
                CmdXoaClick(sender, e);
            }
        }

        public string[] tachhoten(string hoten)
        {
            hoten = hoten.Trim();
            return hoten.Split(' ');
        }
        private void CmdLuuClick(object sender, EventArgs e)
        {
            try
            {
                var ethnic = new Students();
                ethnic.StudyId = int.Parse(cbStudyDisplay.SelectedValue.ToString());
                ethnic.BlockId = int.Parse(cbBlockName.SelectedValue.ToString());
                ethnic.StudyOther = txtStudyOther.Text.Trim();
                if (ckThiNho.Checked)
                {
                    ethnic.UniversityNV1Id = int.Parse(cbUniversityDisplayNV.SelectedValue.ToString());
                    ethnic.StudyNV1Id = int.Parse(cbStudyDisplayNV.SelectedValue.ToString());
                    ethnic.BlockNV1Id = int.Parse(cbBlockDisplayNV.SelectedValue.ToString());
                    ethnic.StudyOtherNV1 = txtStudyOtherNv.Text.Trim();
                }
                ethnic.IsLienThong = checkLienThong.Checked==true;
                var chuoi = tachhoten(txtHoten.Text.Trim());
                var count = chuoi.Length;
                ethnic.FullName = chuoi[count - 1];
                for (var s = 0; s < count - 1; s++)
                {
                    ethnic.LastName += chuoi[s] + " ";
                }
                ethnic.Gender = cbGioiTinh.SelectedText == "Nam";
                ethnic.BirthDay =DateTime.Parse( (cbNgay.SelectedText.Trim() + "/" + cbThang.SelectedText.Trim() + "/" + cbNam.SelectedText.Trim()));



                ethnic.BlockId = int.Parse(cbBlockName.SelectedValue.ToString());
                ethnic.StudyOther = txtStudyOther.Text.Trim();



                DataTable listEthnic = Operator.Operator.GetListDistrictById(ma);
                if (listEthnic.Rows.Count > 0)
                {
                    ethnic.Id = ma;
                    int m = ethnic.Update(ethnic);
                    if (m > 0)
                    {
                        frmThiSinh_Load(sender, e);
                    }
                }
                else
                {
                    int m = ethnic.Insert(ethnic);
                    if (m > 0)
                    {
                        frmThiSinh_Load(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void CmdXoaClick(object sender, EventArgs e)
        {
            try
            {
                DialogResult DRL;
                DRL = MessageBox.Show(Resources.Delete, Resources.Header, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (DRL == DialogResult.No)
                {
                    return;
                }
                else
                {
                    var ethnic = new Students();
                    ethnic.Id = ma;
                    int m = ethnic.Delete(ethnic);
                    if (m > 0)
                    {
                        frmThiSinh_Load(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }
    }
}